"use strict";
/**
 * @ Author: Yoann Meclot. MSay2
 * @ Created on: 2021-06-14 17:49:33
 * @ Modified on: 2021-06-14 17:57:07
 */
/**
 * Copyright (c) 2020-2021 MSay2 (Yoann Meclot) - @msay2/tspoet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.Arrays = void 0;
const System_js_1 = require("../System.js");
const ArrayList_js_1 = require("./ArrayList.js");
class Arrays {
    static asList(array) {
        return new ArrayList_js_1.ArrayList(array);
    }
    static sort(array, start, length, comparable) {
        let size = length - start;
        if (size < 2)
            return;
        this.sortArray(array, start, length, start + this.count(array, start, length, comparable), comparable);
    }
    static count(array, start, length, c) {
        let count = start + 1;
        if (count == length)
            return 1;
        if (c.compare(array[count++], array[start]) < 0) {
            while (count < length && c.compare(array[count], array[count - 1]) < 0)
                count++;
            this.reverse(array, start, count);
        }
        else {
            while (count < length && c.compare(array[count], array[count - 1]) >= 0)
                count++;
        }
        return count - start;
    }
    static reverse(a, start, length) {
        length--;
        while (start < length) {
            let t = a[start];
            a[start++] = a[length];
            a[length--] = t;
        }
    }
    static sortArray(array, start, length, start1, c) {
        if (start1 == start)
            start1++;
        for (; start1 < length; start1++) {
            let pivot = array[start1];
            let left = start;
            let right = start1;
            while (left < right) {
                let mid = (left + right) >>> 1;
                if (c.compare(pivot, array[mid]) < 0)
                    right = mid;
                else
                    left = mid + 1;
            }
            let n = start1 - left;
            switch (n) {
                case 2: array[left + 2] = array[left + 1];
                case 1:
                    array[left + 1] = array[left];
                    break;
                default:
                    System_js_1.System.arraycopy(array, left, array, left + 1, n);
            }
            array[left] = pivot;
        }
    }
}
exports.Arrays = Arrays;
//# sourceMappingURL=Arrays.js.map